// pages/login/phone/phone.js
import Toast from '@vant/weapp/toast/toast';
import {
    request
} from '../../../utils/api.js'
import {
    checkPhone
} from '../../../utils/reg.js'
Page({
    /**
     * 页面的初始数据
     */
    data: {
        sendMessage: "发送验证码",
        disabled: false,
        phone: '',
        message: '',
        code: '',
        userPwd: ''
    },
    async send() {
        this.setData({
            message: []
        })
        let reg = checkPhone(this.data.phone)
        if (reg !== true) {
            this.setData({
                [`message[0]`]: reg
            })
            return
        }

        let res = await request({
            url: '/public/sendPhoneCode?phone=' + this.data.phone,
            method: 'get'
        })
        if (res.code == 200) {
            Toast("验证码发送成功")
            let time = 60
            this, this.setData({
                disabled: true
            })
            setInterval(() => {
                if (time > 0) {
                    time--
                    this.setData({
                        sendMessage: time + '秒后继续发送'
                    })
                } else {
                    this.setData({
                        disabled: false,
                        sendMessage: '发送验证码'
                    })
                }
            }, 1000);
        } else {
            Toast(res.message)
        }
    },
    async next() {
        this.setData({
            message: []
        })
        let reg = checkPhone(this.data.phone)
        if (reg !== true) {
            this.setData({
                [`message[0]`]: reg
            })
            return
        }
        if (!this.data.code) {
            this.setData({
                [`message[1]`]: '请输入验证码'
            })
            return
        }
        if (!this.data.userPwd) {
            this.setData({
                [`message[2]`]: '请输入密码'
            })
            return
        }
        let res = await request({
            url: `/public/verCode?phone=${this.data.phone}&code=${this.data.code}`,
            method: 'get'
        })

        if (res.code == 200) {
            wx.navigateTo({
                url: `/pages/login/role/role?phone=${this.data.phone}&userPwd=${this.data.userPwd}`,
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})